package com.example.animationdrawabledemo;

import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TestView extends View {
	
	private static final String TAG = TestView.class.getSimpleName();
	Bitmap mutable;
	Paint paint;
	Bitmap mask,bgNor,grain;
	Canvas canvas;
	Bitmap bitmap;
	NinePatchDrawable ninePatchDrawable,maskNinePatchDrawable;
	private Rect rect;
	public TestView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mask = BitmapFactory.decodeResource(getResources(), R.drawable.aio_friend_bg_mask);
		bgNor = BitmapFactory.decodeResource(getResources(), R.drawable.aio_friend_bg_nor);
		ninePatchDrawable = new NinePatchDrawable(getResources(), bgNor, bgNor.getNinePatchChunk(), new Rect(), null);
		maskNinePatchDrawable = new NinePatchDrawable(getResources(), mask, mask.getNinePatchChunk(), new Rect(), null);
		grain =BitmapFactory.decodeResource(getResources(), R.drawable.aio_textrue_nor);
	}

	public TestView(Context context, AttributeSet attrs) {
		this(context, attrs,0);
		// TODO Auto-generated constructor stub
	}

	public TestView(Context context) {
		this(context, null,0);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		rect = new Rect(0,0,w,h);
		mutable = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
	}
	
	public void setMask(Bitmap mask) {
		this.mask = mask;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	private long start = 0;
	@Override
	public void draw(Canvas onDrawCanvas) {
		super.draw(onDrawCanvas);
		start = System.currentTimeMillis();
		Log.v(TAG, "1:"+(System.currentTimeMillis()-start));
		canvas = new Canvas(mutable);
		paint = new Paint();
		paint.setFilterBitmap(false);
//		AvoidXfermode avoid = new AvoidXfermode(Color.TRANSPARENT, 255, AvoidXfermode.Mode.AVOID);    
//		Paint paint = new Paint();
//		paint.setColor(Color.BLACK);
//		canvas.drawBitmap(mask, 0, 0, paint);
//		canvas.drawPaint(paint); // actually drawing a bitmap here
//		paint.setXfermode(new AvoidXfermode(Color.BLACK, 0, AvoidXfermode.Mode.TARGET));
//		paint.setColor(Color.GREEN);
//		canvas.drawPaint(paint);
	
		// Copy foggyWindowBmp into tempBitmap
//		canvas.drawBitmap(mutable, 0, 0, null);
//
//	    // Create a radial gradient
//	    RadialGradient gradient = new android.graphics.RadialGradient(
//	            0, 0,
//	            20, 0xFF000000, 0x00000000,
//	            android.graphics.Shader.TileMode.CLAMP);
//
		maskNinePatchDrawable.setBounds(rect);
		maskNinePatchDrawable.draw(canvas);
		Log.v(TAG, "2:"+(System.currentTimeMillis()-start));
		
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		if(bitmap == null){
			bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.aio_textrue_nor);
			Log.v(TAG, "3:"+(System.currentTimeMillis()-start));
			createRepeater(rect.width(),rect.height(),bitmap,canvas,paint);
		}
//		paint.setShader(new BitmapShader(grain,TileMode.REPEAT,TileMode.REPEAT));
//		canvas.drawRect(rect, paint);
		Log.v(TAG, "4:"+(System.currentTimeMillis()-start));
		
		ninePatchDrawable.setBounds(rect);
		ninePatchDrawable.draw(canvas);
		
		Log.v(TAG, "5:"+(System.currentTimeMillis()-start));
		onDrawCanvas.drawBitmap(mutable, 0, 0, null);
		
		Log.v(TAG, ""+(System.currentTimeMillis()-start));
		
		
//		start = System.currentTimeMillis();
//		ninePatchDrawable.setBounds(new Rect(0, 0, getWidth(), getHeight()));
//		ninePatchDrawable.draw(onDrawCanvas);
//		Log.v(TAG, ""+(System.currentTimeMillis()-start));
		
	}
	
	public static void createRepeater(int width,int height,Bitmap src,Canvas canvas,Paint paint) {
		int count = (width + src.getWidth() - 1) / src.getWidth();
		int heightcount = (height + src.getHeight() - 1) / src.getHeight();
		for (int idx = 0; idx < count; ++idx) {
			for(int idx2 = 0; idx2 < heightcount; ++idx2){
				canvas.drawBitmap(src, idx * src.getWidth(), idx2 * src.getHeight(), paint);
			}
		}
	}

}
